﻿//定义cookie中key值
var cookieId = "ccbcustomid"; //长效cookieId
var custId = "cs_cid";        //会员ID
var tagInfoId = "tagInfoId";  //客户标签
var tagInfoCustId = "tagInfoCustId"  //"查询客户标签对应的custId"
var lastUpdateTimeId = "lastUpdateTime";         //上次更新时间
var cookieidTagFlagId = "cookieidTagFlag";         //当只根据cookieid未根据custId进行查询的标签时，此时cookieTagFlag为1；否则为0。
var expire = 24;			  //标签过期时间，单位为小时3*24。
var cookieExpire = 30;        //标签cookie过期时间，单位为天。


$(function(){
   var url = window.location.href;
   if(url.indexOf('login')<0){getTags();}
});
function innerFinanceTest(n) {
  switch(n){
    case 1:
	document.write('<div id="adsame-10108" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://ccb.com/cn/html1/office/xyk/subject/16/0804xjfq/index.html" target="_blank" class="AcqAdvClass" acqadid="website_creditcard_现金分期七折惠_01"><img src="http://image3.ccb.com/cn/v3/images/advPictrue/p6.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 2:
	document.write('<div id="adsame-10109" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://store.ccb.com/cn/html1/office/xyk/subject/15/0701lkxql/index.html" target="_blank" class="AcqAdvClass" acqadid="website_creditcard_龙卡星期六_001"><img src="http://image3.ccb.com/cn/v3/images/advPictrue/p7.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 3:
	document.write('<div id="adsame-10110" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://store.ccb.com/cn/html1/office/fund/subject/15/0908jxsy/index.html" target="_blank" class="AcqAdvClass" acqadid="website_finance_速盈_001"><img src="http://image3.ccb.com/cn/v3/images/advPictrue/p5.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 4: 
	document.write('<div id="adsame-10111" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://finance.ccb.com/cn/finance/productnews/newsdetail/20161014_8905127.html" target="_blank" class="AcqAdvClass" acqadid="website_finance_“乾元”代工专享第7期_mrggdz16101404"><img src="http://image3.ccb.com/cn/v3/images/advPictrue/p1.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 5:
	document.write('<div id="adsame-10112" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://ehome.ccb.com/Channel/37044120" target="_blank" class="AcqAdvClass" acqadid="website_personalloan_默认_002"><img src="http://image3.ccb.com/cn/v3/images/advPictrue/p3.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 6:
	document.write('<div id="adsame-10113" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="https://ibsbjstar.ccb.com.cn/CCBIS/V6/STY1/CN/login2.jsp" target="_blank" class="AcqAdvClass" acqadid="website_personalloan_默认_001"><img src="http://image3.ccb.com/cn/v3/images/advPictrue/p4.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 7:
	document.write('<div id="adsame-10114" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://login.ccb.com/tran/WCCMainPlatV5?CCB_IBSVersion=V5&SERVLET_NAME=WCCMainPlatV5&TXCODE=NHY010&tourl=http://group.ccb.com/cn/home/indexv3.html" target="_blank" class="AcqAdvClass" acqadid="website_personalebanking_我的com_001"><img src="http://image4.ccb.com/cn/v3/images/advPictrue/p8.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 8:
	document.write('<div id="adsame-10115" style="width:280px;height:210px;border:none;padding:0px;margin:0px;overflow:hidden;position:relative;"><a href="http://store.ccb.com/cn/public/20150812_1439361231.html" target="_blank" class="AcqAdvClass" acqadid="website_personalebanking_e账户_001"><img src="http://image4.ccb.com/cn/v3/images/advPictrue/p2.jpg" style="width:280px;height:210px;border:none;" /></a></div>');
	break;
	case 9:
	document.write('<li>');
	document.write('<a href="http://ccb.com/cn/html1/office/ebank/dzb/subject/16/0908jhlygwy/index.html" class="index_x_banner_main_img_left AcqAdvClass" target="_blank" acqadid="" id="adv_6012" >');
	document.write('<img src="http://image4.ccb.com/cn/v3/images/advPictrue/pic2.png"></a>');
	document.write('<div class="index_x_banner_main_box">');
	document.write('<a href="http://ccb.com/cn/html1/office/ebank/dzb/subject/16/0908jhlygwy/index.html" target="_blank" acqadid="" id="adv_6012_A" >');
	document.write('建行0元购<br>你购物 </a> ');
	document.write('<span class="index_x_banner_main_box_value">我买单</span> </div>');
	document.write('</li>'); 
	var acqadid= '互联网站_personalebanking_mrggdz16093001_';
	document.getElementById('adv_6012').setAttribute('acqadid',acqadid);
	document.getElementById('adv_6012_A').setAttribute('acqadid',acqadid);
	break;
	case 10:
	document.write('<li>');
	document.write('<a href="http://sale.buy.ccb.com/channel/xdmp_161020/index.html" class="index_x_banner_main_img_left AcqAdvClass" target="_blank" acqadid="" id="adv_6106" >');
	document.write('<img src="http://image4.ccb.com/cn/v3/images/advPictrue/pic3.png"></a>');
	document.write('<div class="index_x_banner_main_box">');
	document.write('<a href="http://sale.buy.ccb.com/channel/xdmp_161020/index.html" target="_blank" acqadid="" id="adv_6106_A" >');
	document.write('星店名品<br>日本好货</a> ');
	document.write('<span class="index_x_banner_main_box_value">全场低至9.9元</span> </div>');
	document.write('</li>');
	var acqadid= '互联网站_善融商务_日本好货_mrggdz16101903';
	document.getElementById('adv_6106').setAttribute('acqadid',acqadid);
	document.getElementById('adv_6106_A').setAttribute('acqadid',acqadid);
	break;
	case 11:
	document.write('<li>');
	document.write('<a href="http://life.ccb.com/cn/paymentv3/bill_item/201504231039322989.html" class="index_x_banner_main_img_left AcqAdvClass" target="_blank" acqadid="" id="adv_4664" >');
	document.write('<img src="http://image4.ccb.com/cn/v3/images/advPictrue/pic1.png"></a>');
	document.write('<div class="index_x_banner_main_box">'); 
	document.write('<a href="http://life.ccb.com/cn/paymentv3/bill_item/201504231039322989.html" target="_blank" acqadid="" id="adv_4664_A" >');
	document.write('话费打折<br>极速到账</a> ');
	document.write('<span class="index_x_banner_main_box_value">多方报价 给你最优</span> </div>');
	document.write('</li>');
	var acqadid= 'website_personalebanking__';
	document.getElementById('adv_4664').setAttribute('acqadid',acqadid);
	document.getElementById('adv_4664_A').setAttribute('acqadid',acqadid);
	break;
	default:
	console.log("nopic");
	break;
  }
}
function setCookieAdv(name,value,expires){
	var days = expires;
	var exp = new Date();
	exp.setTime(exp.getTime() + days*24*3600*1000);
	str = name + "=" +escape(value) + ";expires=" + exp.toGMTString()+";path=/;domain=.ccb.com";
	document.cookie = str;
}

function getCookieAdv(name){
	var arr,reg = new RegExp("(^|)"+name +"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return "";
}

function adv(id,type){
	try{
		var tagInfo = getCookieAdv(tagInfoId);
		if(type=='a'){
			if(tagInfo!=null){
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advall?z=advccb&c="+id+tagInfo+"' charset='gbk'></script>");						
			}else{
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advall?z=advccb&c="+id+"' charset='gbk'></script>");
			}
		}
		if(type=='s'){
			if(tagInfo!=null){
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advshow?z=advccb&c="+id+tagInfo+"' charset='gbk'></script>");						
			}else{
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advshow?z=advccb&c="+id+"' charset='gbk'></script>");
			} 
		}
		if(type=='c'){
		    if(tagInfo!=null){
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advall?z=advccb&op=1&c="+id+tagInfo+"' charset='gbk'></script>");						
			}else{
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advall?z=advccb&op=1&c="+id+"' charset='gbk'></script>");
			}
		
		
		} 
		if(type=='h'){
			if(tagInfo!=null){
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advshow?z=advccb&op=1&c="+id+tagInfo+"' charset='gbk'></script>");						
			}else{
				document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advshow?z=advccb&op=1&c="+id+"' charset='gbk'></script>");
			} 
		}
	}catch(e){}
}

function advParam(id,type,param){
	try{
		var tagInfo = getCookieAdv(tagInfoId);
		if(param!=null && param.length>0){
			tagInfo = tagInfo + '&'+param;
			if(type=='a'){
				if(tagInfo!=null){
					document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advall?z=advccb&c="+id+tagInfo+"' charset='gbk'></script>");						
				}else{
					document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advall?z=advccb&c="+id+"' charset='gbk'></script>");
				}
			}
			if(type=='s'){
				if(tagInfo!=null){
					document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advshow?z=advccb&c="+id+tagInfo+"' charset='gbk'></script>");						
				}else{
					document.writeln("<script type='text/javascript' src='http://adv.ccb.com/advshow?z=advccb&c="+id+"' charset='gbk'></script>");
				}
			}
		}else{
			adv(id,type);
		}
	}catch(e){}
}

//---------------------------------------------------  
// 获取广告请求的地址  
// advid:广告位id  
// advtype：a/all代码  s/show代码 
// requesttype： js/js请求代码  iframe/iframe请求代码
// param：附加在广告请求后面的额外参数，多个字段用&进行连接
//---------------------------------------------------  
function getAdvUrl(advid,advtype,requesttype,param){
	var advUrl; 
	var tagInfo = getCookieAdv(tagInfoId);
	if(!advid || !advtype || !requesttype){
		return null;
	}
	if(param){
		tagInfo = tagInfo + '&'+param;
	}
	
	if(advtype=='a'){
		if(tagInfo){
			if(requesttype=='iframe'){
				advUrl = 'http://adv.ccb.com/advall?z=advccb&c='+advid+'&op=1'+tagInfo;
			}else if(requesttype=='js')	{
				advUrl = 'http://adv.ccb.com/advall?z=advccb&c='+advid+tagInfo;
			}			
		}else{
			if(requesttype=='iframe'){
				advUrl = 'http://adv.ccb.com/advall?z=advccb&c='+advid+'&op=1';
			}else if(requesttype=='js'){
				advUrl = 'http://adv.ccb.com/advall?z=advccb&c='+advid;
			}
		}
	}
	if(advtype=='s'){
		if(tagInfo){
			if(requesttype=='iframe'){
				advUrl = 'http://adv.ccb.com/advshow?z=advccb&c='+advid+'&op=1'+tagInfo;
			}else if(requesttype=='js'){
				advUrl = 'http://adv.ccb.com/advshow?z=advccb&c='+advid+tagInfo;
			}						
		}else{
			if(requesttype=='iframe'){
				advUrl = 'http://adv.ccb.com/advshow?z=advccb&c='+advid+'&op=1';
			}else if(requesttype=='js'){
				advUrl = 'http://adv.ccb.com/advshow?z=advccb&c='+advid;
			}
		}
	}
	return advUrl;
}


function getTags(){
	var custCookieId = getCookieAdv(cookieId);
	var custCustid = getCookieAdv(custId);
	var tagInfoCust = getCookieAdv(tagInfoCustId);
	var custTagInfo = getCookieAdv(tagInfoId);
	var lastUpdateTime = getCookieAdv(lastUpdateTimeId);
	var cookieidTagFlag = getCookieAdv(cookieidTagFlagId);
	var url = "http://www.ccb.com/tran/WCCMainPlatV5?CCB_IBSVersion=V5&SERVLET_NAME=WCCMainPlatV5&isAjaxRequest=true&TXCODE=NBQ001"+"&Cookie_Id="+custCookieId+"&Usr_Id="+custCustid;
	
	var end = new Date();
	var start = strToDate(lastUpdateTime);
	var internal = expire;
	if(start!=0){
		internal = DateDiff('h',start,end);
	}
	if(!(custCookieId=="" && custCustid=="")){
		if((cookieidTagFlag == '1' && custCustid !="")||(custCustid!=""&&tagInfoCust!=custCustid)||internal>=expire){	
			$.ajax({
				type: "get",
				cache:false,
				async:true,
				timeout:1500,
				url:url,
				dataType:"json",
				success: function (data) {
					var tagInfoParam = data.tagList;
					var dParam = "";
					if(tagInfoParam.length>0){
						
						for(var i=0;i<tagInfoParam.length;i++){
							dParam = dParam + "&" + tagInfoParam[i].tagId +"="+tagInfoParam[i].tagValue;
						}	
					}
					if(custCustid==""){
						setCookieAdv(cookieidTagFlagId,'1',cookieExpire);  //当只通过cookieId查询标签时，该字段值为1
					}else {
						setCookieAdv(cookieidTagFlagId,'0',cookieExpire);  //custCustId不为空时，该字段值为0
						setCookieAdv(tagInfoCustId,custCustid,cookieExpire);  //custCustId不为空时，该字段值为custCustId
					}
					setCookieAdv(tagInfoId,dParam,cookieExpire);
					setCookieAdv(lastUpdateTimeId,formatDate('YYYY-MM-DD hh:mm:ss'),cookieExpire);
				},
				error: function() {	
					
				}
			});
		}
	}
}

//---------------------------------------------------  
// 日期格式化  
// 格式 YYYY/yyyy/YY/yy 表示年份  
// MM/M 月份   
// dd/DD/d/D 日期  
// hh/HH/h/H 时间  
// mm/m 分钟  
// ss/SS/s/S 秒  
//---------------------------------------------------  
function formatDate(formatStr)   
{   
    var str = formatStr;  
    var now = new Date();
    str=str.replace(/yyyy|YYYY/,now.getFullYear());   
    str=str.replace(/yy|YY/,(now.getYear() % 100)>9?(now.getYear() % 100).toString():'0' + (now.getYear() % 100));   
	var month = now.getMonth()+1;
    str=str.replace(/MM/,month>9?month.toString():'0' + month);   
    str=str.replace(/M/g,month);   
  
    str=str.replace(/dd|DD/,now.getDate()>9?now.getDate().toString():'0' + now.getDate());   
    str=str.replace(/d|D/g,now.getDate());   
  
    str=str.replace(/hh|HH/,now.getHours()>9?now.getHours().toString():'0' + now.getHours());   
    str=str.replace(/h|H/g,now.getHours());   
    str=str.replace(/mm/,now.getMinutes()>9?now.getMinutes().toString():'0' + now.getMinutes());   
    str=str.replace(/m/g,now.getMinutes());   
  
    str=str.replace(/ss|SS/,now.getSeconds()>9?now.getSeconds().toString():'0' + now.getSeconds());   
    str=str.replace(/s|S/g,now.getSeconds());   
  
    return str;   
}   

//---------------------------------------------------  
// 字符串转成日期类型   
// 格式 YYYY-MM-dd hh:mm:ss
// 错误的日期格式返回0 
//---------------------------------------------------  
function strToDate(c_date) {
    if (!c_date)
        return 0;
    var tempArray = c_date.split("-");
    if (tempArray.length != 3) {
        return 0;
    }
    var dateArr = c_date.split(" ");
    var date = null;
    if (dateArr.length == 2) {
        var yymmdd = dateArr[0].split("-"); 
        var hhmmss = dateArr[1].split(":");
        date = new Date(yymmdd[0], yymmdd[1] - 1, yymmdd[2], hhmmss[0], hhmmss[1], hhmmss[2]);
    } else {
        date = new Date(tempArray[0], tempArray[1] - 1, tempArray[2], 00, 00, 01);
    }
    return date;
};

//---------------------------------------------------  
// 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
// interval: s:秒|m:分钟|h:小时|d:天数|w:星期|m:月|y:年
// 错误的参数返回-1 
//---------------------------------------------------  
function DateDiff(interval,start, end) {
	if(interval ==null || start==null || end==null){
		return -1;
	}
    switch (interval) {   
        case 's' :return parseInt((end - start) / 1000);  
        case 'm' :return parseInt((end - start) / 60000);  
        case 'h' :return parseInt((end - start) / 3600000);  
        case 'd' :return parseInt((end - start) / 86400000);  
        case 'w' :return parseInt((end - start) / (86400000 * 7));  
        case 'm' :return (end.getMonth()+1)+((end.getFullYear()-start.getFullYear())*12) - (start.getMonth()+1);  
        case 'y' :return end.getFullYear() - start.getFullYear();  
    }  
}